<template>
    <div class="box">
        <div><h1>用户注册</h1></div>
        <div>
            <el-form
                ref="ruleFormRef"
                style="max-width: 600px"
                :model="ruleForm"
                :rules="rules"
                label-width="auto"
            >
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="ruleForm.mobile" />
                </el-form-item>


                <el-form-item label="手机验证码" required>
                    <el-col :span="11">
                        <el-form-item prop="code">
                            <el-input v-model="ruleForm.code" />
                        </el-form-item>
                    </el-col>
                    <el-col class="text-center" :span="2">
                        <span class="text-gray-500"> </span>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item prop="date2">
                            <el-button type="primary" @click="submitSendCode(ruleFormRef)">发送验证码</el-button>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-form-item label="登录密码" prop="pass">
                    <el-input v-model="ruleForm.pass" />
                </el-form-item>
                
                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">
                        立即注册
                    </el-button>
                    <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { sendCode, userReg } from '@/api/api'
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { useRouter } from "vue-router";

const router = useRouter();

interface RuleForm {
    mobile: string;
    code: string;
    pass: string;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
    mobile: "",
    code: "",
    pass: "",
});

const locationOptions = ["Home", "Company", "School"];

const rules = reactive<FormRules<RuleForm>>({
    mobile: [
        {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
        },
        { pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: "请输入正确的手机号", trigger: "blur" },
    ],
    code: [
        {
            required: true,
            message: "请输入验证码",
            trigger: "blur",
        },
    ],
    pass: [
        {
            required: true,
            message: "请输入登录密码",
            trigger: "blur",
        },
    ],
});


// 发送验证码
const submitSendCode = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validateField(['mobile'], (valid) => {
        if (valid) {
            // 表单校验通过
            sendCode(ruleForm.mobile).then(res => {
                if(res.data.code == 200) {
                    ElMessage.success('验证码发送成功');
                }
                else {
                    ElMessage.error(res.data.msg);
                }
            })
        } else {
            // 表单校验失败了
            console.log("error submit!");
        }
    });
};

// 立即注册
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((valid, fields) => {
        if (valid) {
            // 表单校验通过
            userReg(ruleForm).then(res => {
                if(res.data.code == 200) {
                    ElMessage.success('注册成功');
                    router.push('/login');
                }
                else {
                    ElMessage.error(res.data.msg);
                }
            })
        } else {
            // 表单校验不通过
            console.log("error submit!", fields);
        }
    });
};

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}));
</script>
